package com.red.wine.ui;

import java.io.IOException;

import com.baidu.mapapi.utils.e;
import com.google.json.Gson;
import com.google.json.JsonParseException;
import com.red.wine.R;
import com.red.wine.WineApp;
import com.red.wine.model.DataInfo;
import com.red.wine.model.Headers;
import com.red.wine.model.RegisterData;
import com.red.wine.model.RegisterReq;
import com.red.wine.model.RegisterReturn;
import com.red.wine.model.ReturnInfo;
import com.red.wine.model.TasteRequest;
import com.red.wine.util.IntentUtil;
import com.red.wine.util.PrefsUtils;
import com.red.wine.util.StringUtil;
import com.red.wine.webservice.ServiceAsynTask;
import com.red.wine.webservice.ServiceHelper;
import com.red.wine.webservice.ServiceHost;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class RegisterActivity extends BaseActivity {
	private ImageView back;
	private EditText name;
	private EditText pass;
	private EditText email;
	private Button button;
	private Context context;
	private TextView title;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register_layout);
		context = this;
		back = (ImageView) findViewById(R.id.loginback);
		name = (EditText) findViewById(R.id.put_name);
		pass = (EditText) findViewById(R.id.put_pass);
		email = (EditText) findViewById(R.id.put_phone);
		button = (Button) findViewById(R.id.register_btns);
		title= (TextView) findViewById(R.id.register_or_login_title);

		back.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				IntentUtil.redirectToNext(context, LoginActivity.class);
				finish();
			}
		});
		
		button.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				if(WineApp.getInstance().isNetworkAvailable() || WineApp.getInstance().isWiFiActive()){
					String getName = name.getText().toString().trim();
					if(!StringUtil.isEmptyOrNull(getName)){
						if(!StringUtil.isLetterDigitOrChinese(getName)){
							Toast.makeText(context, "用户名输入不合法", Toast.LENGTH_SHORT).show();
							return;
						}
					}else{
						Toast.makeText(context, "用户名为空", Toast.LENGTH_SHORT).show();
						return;
					}
					String getEmail = email.getText().toString().trim();
					if(!StringUtil.isEmptyOrNull(getEmail)){
						if(!StringUtil.checkEmail(getEmail)){
							Toast.makeText(context, "邮箱输入有误", Toast.LENGTH_SHORT).show();
							return;
						}
					}else{
						Toast.makeText(context, "邮箱为空", Toast.LENGTH_SHORT).show();
						return;
					}
					String getPass = pass.getText().toString().trim();
					if(!StringUtil.isEmptyOrNull(getPass)){
						if(!StringUtil.isLetterDigitOrChinese(getPass)){
							Toast.makeText(context, "密码输入不合法", Toast.LENGTH_SHORT).show();
							return;
						}
					}else{
						Toast.makeText(context, "密码为空", Toast.LENGTH_SHORT).show();
						return;
					}
					
					getData(getName, getPass, getEmail);
				}else{
					Toast.makeText(context, "无可用网络", Toast.LENGTH_SHORT).show();
				}
			}
		});
	}

	private void getData(final String name, final String pass, final String email){
		showLoading();
		new ServiceAsynTask<RegisterReturn>() {

			@Override
			protected RegisterReturn callService() throws IOException,
					JsonParseException, AssertionError {
				RegisterReq taste = new RegisterReq();
				Headers header = new Headers();
				header.setUa("ANDROID_HY/2.0.1.0 (customerid=gypsii, variant=gypsii)");

				RegisterData data = new RegisterData();
				data.setName(name);
				data.setEmail(email);
				data.setPass(pass);

				taste.cmd = "user_reg";
				taste.headers = header;
				taste.data = data;
//				taste.sid = "";

				String up = new Gson().toJson(taste);
				String send = StringUtil.unicodeToUtf8("json=" + up);
				String urlstr = ServiceHost.getTasteURL();

				return ServiceHelper.getInstance().postData(urlstr, send,
						RegisterReturn.class);
			}

			@Override
			protected void runWithResult(RegisterReturn result) throws Exception {
				
				if (result != null) {
					bindData(result, name, pass);
					Log.e("TAG", "isnotnull");
				} else {
//					Log.e("TAG", "is null");
					PrefsUtils.setHaseRegister(null);
					Toast.makeText(context, "注册失败", Toast.LENGTH_SHORT).show();
				}
				hideLoading();
			}

			@Override
			protected void runWithError(RegisterReturn result, int iErrorCode)
					throws Exception {
				Log.e("TAG", "异常");
				PrefsUtils.setHaseRegister(null);
				IntentUtil.redirectToNext(context, LoginActivity.class);
				finish();
				Toast.makeText(context, "注册失败", Toast.LENGTH_SHORT).show();
				hideLoading();
			}
		}.execute();
	}
	
	private void bindData(RegisterReturn result, String name, String pass){
		if(result.getRsp().equals("1")){
			PrefsUtils.setFirst("1");
			Toast.makeText(context, "注册成功", Toast.LENGTH_SHORT).show();
			PrefsUtils.setHaseRegister(null);
			PrefsUtils.setName(name);
			PrefsUtils.setPass(pass);
			IntentUtil.redirectToNext(context, PerInfoActivity.class);
			finish();
		}else{
			PrefsUtils.setHaseRegister(null);
			Toast.makeText(context, "注册失败", Toast.LENGTH_SHORT).show();
			return;
		}
		
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			IntentUtil.redirectToNext(context, LoginActivity.class);
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}
}
